package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessQueryDTO;
import com.qk.dto.BusinessUpdateDTO;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 28188
 */
@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @PostMapping
    public Result addBusiness(@RequestBody Business business) {
        log.info("新增商机: {}", business);
        businessService.saveBusiness(business);
        return Result.success();
    }

    @GetMapping
    public Result pageBusinesses(BusinessQueryDTO businessQueryDTO) {
        log.info("查询商机: {}", businessQueryDTO);
        PageResult<Business> businessPage = businessService.pageBusinesses(businessQueryDTO);
        return Result.success(businessPage);
    }

    @PutMapping("/assign/{businessId}/{userId}")
    public Result assignBusiness(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配商机: 商机ID={},用户ID={}", businessId, userId);
        businessService.assignBusiness(businessId, userId);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id) {
        log.info("查询商机: {}", id);
        Business businesses = businessService.getBusinessById(id);
        return Result.success(businesses);
    }

    @PutMapping
    public Result updateBusiness(@RequestBody BusinessUpdateDTO businessUpdateDTO) {
        log.info("跟进商机: {}", businessUpdateDTO);
        businessService.updateBusinessById(businessUpdateDTO);
        return Result.success();
    }

    @PutMapping("/back/{id}")
    public Result backBusiness(@PathVariable Integer id) {
        log.info("踢回公海: {}", id);
        businessService.backBusiness(id);
        return Result.success();
    }

    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        log.info("商机转客户: {}", id);
        businessService.toCustomer(id);
        return Result.success();
    }

    @GetMapping("/pool")
    public Result pagePool(BusinessQueryDTO businessQueryDTO) {
        log.info("查询公海池: {}", businessQueryDTO);
        PageResult<Business> businessPage = businessService.pagePool(businessQueryDTO);
        return Result.success(businessPage);
    }

}
